package com.appkubes.fintech.restful.config;

import com.appkubes.fintech.common.response.Response;
import com.appkubes.fintech.core.po.FintechRentBillHeader;
import com.appkubes.fintech.restful.application.rent.service.impl.RePayServiceImple;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Jiahong Chen
 * @date 2019/11/9
 */
@EnableScheduling
@Configuration
@Slf4j
public class ScheduleConfig {

    @Resource
    RePayServiceImple rePayService;

    @Autowired
    protected BaseMapper<FintechRentBillHeader> baseMapper;

    @Scheduled(cron = "0 * * * * ? ")
    public void repayFirstTime() {
        try {
            System.out.println("repay订单超时定时器启动");
            log.info("repay订单超时定时器启动");
            QueryWrapper<FintechRentBillHeader> queryWrapper = new QueryWrapper<>();
            FintechRentBillHeader queryOrder = new FintechRentBillHeader();
            queryOrder.setIsPayFirst(1);
            queryOrder.setUnpayPeriods(1);
            queryWrapper.setEntity(queryOrder);

            List<FintechRentBillHeader> headerList = baseMapper.selectList(queryWrapper);
            log.info("repay订单超时定时器正在执行。。。。。。,共有单：" + headerList.size());
            headerList.forEach(header -> {
                log.info("repay订单超时定时器==>" + header.getBillKey());
                Response res = rePayService.repayFirst(header.getBillKey());
                if (res.getCode() != 200) {
                    log.info("repay订单超时定时器错误==>" + header.getBillKey());
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            log.error("repay订单超时定时器错误", e);
        }
    }
}
